package io.github.maojian.demo;

/**
 * 请勿改变本Java类的包名，类名，方法名，方法参数类型，方法返回值类型
 */

public class StringDecode {

    /**
     * 返回解码后的字符串原文，本方法按照竞赛题目要求完成
     *
     * @param keyStr
     *            给定的keyStr字符串
     * @param key
     *            给定的正整数key
     * @param encodedStr 已加密的密文
     * @return 返回解码后的字符串原文
     */
    public String decode(String keyStr, int key, String encodedStr) {
        String tab1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        int length = 26;
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < 26; i++) {
            if (!keyStr.contains("" + tab1.charAt(i))) {
                stringBuilder.append(tab1.charAt(i));
            }
        }
        String first = stringBuilder.substring(0, length - keyStr.length() - key + 1);
        String second = stringBuilder.substring(length - keyStr.length() - key + 1);
        String tab2 = second + keyStr + first;
        StringBuilder decodeStr = new StringBuilder();
        for (int i = 0; i < encodedStr.length(); i++) {
            int index = tab2.indexOf(encodedStr.charAt(i));
            decodeStr.append(tab1.charAt(index));
        }
        return decodeStr.toString();
    }

    public static void main(String[] args) {
        StringDecode func = new StringDecode();
        String ret = func.decode("NORTH", 9, "ABEGZRNAY");
        System.out.println(ret);
    }

}
